package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.utils.SMSUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void sendMsg(String phone) {
        //1.生成6位随机数字作为验证码
        String code = RandomStringUtils.randomNumeric(6);

        System.out.println("code = " + code);
        System.out.println("phone = " + phone);
        //2.调用发送短信的工具类，发送短信
        SMSUtils.send(code,phone);

        //3.将生成的验证码保存在redis中，用统一前缀+手机号，作为唯一的key
        stringRedisTemplate.opsForValue().set(RedisConstant.APP_LOGIN_PREFIX+phone,code,RedisConstant.APP_LOGIN_CODE_TTL_SEC, TimeUnit.SECONDS);
    }
}
